<?php

// +----------------------------------------------------------------------
// | Admin Plugin for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2024 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/think-plugs-admin
// | github 代码仓库：https://github.com/zoujingli/think-plugs-admin
// +----------------------------------------------------------------------

declare(strict_types=1);

namespace app\cms\controller;

use app\cms\model\StockLogs;
use app\cms\model\StockSlices;
use app\cms\model\StockVacuum;
use think\admin\Controller;
use think\admin\helper\QueryHelper;

/**
 * 果片管理
 * @class StockSlice
 * @package app\cms\controller
 */
class StockSlice extends Controller
{
    /**
     * 果片管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        StockSlices::mQuery()->layTable(function () {
            $this->title = '果片管理';
            $this->types = [];
        }, static function (QueryHelper $query) {
            $query->order('create_at', 'desc');
            $query->where(['deleted' => 0]);
        });
    }

    /**
     * 果片详情
     * @auth true
     */
    public function info()
    {

        $this->info = StockSlices::mk()->where(['id' => $this->request->get('id')])->find();
        $logs = StockLogs::mk()->where(['slice_id' => $this->request->get('id')])->select()->toArray();
        $vacuums = StockVacuum::mk()->where(['slice_id' => $this->request->get('id')])->select()->toArray();
        

        $logs = StockLogs::mk()->where(['slice_id' => $this->request->get('id')])
            ->alias('a')
            ->join('stock_goods w', "a.goods_id = w.id")
            ->select()->toArray();
        //logs 按照时间分组

        $new_arr = array_merge($vacuums,$logs);

        
        $grouped = [];

        foreach ($new_arr as $item) {

            $date = $item['create_date'];

            if (!isset($grouped[$date])) {

                $grouped[$date] = [];
            }

            $grouped[$date][] = $item;
        }

        ksort($grouped);
        //var_dump($grouped);die;
        $this->logs = $grouped;
        //var_dump($this->logs);die;
        $this->fetch();
    }

    /**
     * 添加数据
     * @auth true
     */
    public function add()
    {
        StockSlices::mForm('form');
    }

    /**
     * 编辑数据
     * @auth true
     */
    public function edit()
    {
        StockSlices::mForm('form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DbException
     */
    protected function _form_filter(array &$data)
    {

        if ($this->request->isPost()) {
            // A non-numeric value encountered 
            $data['total_weight'] = (int)$data['box_num'] * 420 + (float)$data['bulk_weight'];
            //$data['total_weight'] = $data['box_num'] * 420 + $data['bulk_weight'];
        }else{
            $data['create_date'] = !empty($data['create_date']) ? $data['create_date'] : date('Y-m-d');
        }
       
    }

    /**
     * 编辑真空
     * @auth true
     */
    public function stateOne()
    {

        $data = $this->_vali([
            'status_one.in:0,1'  => '状态值范围异常！',
            'status_one.require' => '状态值不能为空！',
            'id.require' => '状态值不能为空！',
        ]);
        if ($data['status_one'] == 1) {
            StockVacuum::mk()->insert([
                'slice_id' => $data['id'],
                'type' => 1,
                'create_date' => date('Y-m-d'),
                'name' => '第一次入真空;'
            ]);
            StockSlices::mSave($data);
        }
        
        
        
    }


    /**
     * 编辑真空
     * @auth true
     */
    public function stateTwo()
    {
        $data = $this->_vali([
            'status_two.in:0,1'  => '状态值范围异常！',
            'status_two.require' => '状态值不能为空！',
            'id.require' => '状态值不能为空！',
        ]);
        if ($data['status_two'] == 1) {
            StockVacuum::mk()->insert([
                'slice_id' => $data['id'],
                'type' => 1,
                'create_date' => date('Y-m-d'),
                'name' => '第二次入真空;'
            ]);
            StockSlices::mSave($data);
        }
        
    }

    
}